using System;
using System.Linq;

using Abmemunit.Domain.Users;

namespace Abmemunit.Services.Users
{
    public static class UserExtentions
    {
        /// <summary>
        /// Gets a value indicating whether user is in a certain user role
        /// </summary>
        /// <param name="user"></param>
        /// <param name="roleName"></param>
        /// <param name="onlyActiveRoles"></param>
        /// <returns></returns>

        public static bool IsInUserRole(this User user,
            string roleName, bool onlyActiveRoles = true)
        {
            if (user == null)
                return false;

            if (String.IsNullOrEmpty(roleName))
                return false;

            var result = user.UserRoles
                .Where(cr => !onlyActiveRoles || cr.Active)
                .FirstOrDefault() != null;
            return result;
        }


        /// <summary>
        /// Gets a value indicating whether user is administrator
        /// </summary>
        /// <param name="user"></param>
        /// <param name="onlyActiveRoles"></param>
        /// <returns></returns>
        public static bool IsAdmin(this User user, bool onlyActiveRoles = true)
        {
            return IsInUserRole(user, UserRoleNames.Administrators, onlyActiveRoles);
        }

        /// <summary>
        /// Gets a value indicating whether user is a member
        /// </summary>
        /// <param name="user"></param>
        /// <param name="onlyActiveRoles"></param>
        /// <returns></returns>
        public static bool IsMember(this User user, bool onlyActiveRoles = true)
        {
            return IsInUserRole(user, UserRoleNames.Members, onlyActiveRoles);
        }

    }
}
